package com.sj.purchase.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * Created by icodebug on 17-2-9.
 */
public class WmsUtil {
    private static String WMS_URL = "http://192.168.2.8:3333/Security/Router/Gateway.svc";
    private static String SESSION_ID;

    public static void Login(){
        String param = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <s:Body>\n" +
                "    <CallSearchMethod xmlns=\"http://tempuri.org/\">\n" +
                "      <systemTypeCode>WMS</systemTypeCode>\n" +
                "      <companyNo>GuanyiSoft</companyNo>\n" +
                "      <servicePort>SYS_LOGIN</servicePort>\n" +
                "      <languageCode>Chinese</languageCode>\n" +
                "      <currentUser>孙金环</currentUser>\n" +
                "      <password>888888</password>\n" +
                "      <currentModule/>\n" +
                "      <queryConditions xmlns:a=\"Gysoft.Wms.Core.LinqQuery\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:nil=\"true\"/>\n" +
                "      <orderFields xmlns:a=\"Gysoft.Wms.Core.LinqQuery\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:nil=\"true\"/>\n" +
                "      <currentPage>1</currentPage>\n" +
                "      <pageSize>1</pageSize>\n" +
                "    </CallSearchMethod>\n" +
                "  </s:Body>\n" +
                "</s:Envelope>";
        JSONObject obj = postWms(param).getJSONObject(0);
        SESSION_ID = obj.getString("fSessionID");
        System.out.println(SESSION_ID);
    }


    /**
     * 库存汇总
     * @return
     */
    public static JSONArray getKchz(){
        String param = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <s:Body>\n" +
                "    <CallSearchMethod xmlns=\"http://tempuri.org/\">\n" +
                "      <systemTypeCode>WMS</systemTypeCode>\n" +
                "      <companyNo>GuanyiSoft</companyNo>\n" +
                "      <servicePort>SearchInventorySum</servicePort>\n" +
                "      <languageCode>Chinese</languageCode>\n" +
                "      <currentUser>孙金环</currentUser>\n" +
                "      <password>"+SESSION_ID+"</password>\n" +
                "      <currentModule/>\n" +
                "      <queryConditions xmlns:a=\"Gysoft.Wms.Core.LinqQuery\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "        <a:QueryStructure>\n" +
                "          <a:DataType>Guid</a:DataType>\n" +
                "          <a:FieldName>fStorerID</a:FieldName>\n" +
                "          <a:FieldValue>e7788baa-bcd1-4692-a2be-7e916d5097f9</a:FieldValue>\n" +
                "          <a:Operator>Equal</a:Operator>\n" +
                "          <a:Relation>And</a:Relation>\n" +
                "        </a:QueryStructure>\n" +
                "        <a:QueryStructure>\n" +
                "          <a:DataType>Guid</a:DataType>\n" +
                "          <a:FieldName>fWarehouseID</a:FieldName>\n" +
                "          <a:FieldValue>b598fb6d-cd87-4163-a07e-5dd4313853f4</a:FieldValue>\n" +
                "          <a:Operator>Equal</a:Operator>\n" +
                "          <a:Relation>And</a:Relation>\n" +
                "        </a:QueryStructure>\n" +
                "        <a:QueryStructure>\n" +
                "          <a:DataType>String</a:DataType>\n" +
                "          <a:FieldName>fZoneTypeCode</a:FieldName>\n" +
                "          <a:FieldValue>Picking</a:FieldValue>\n" +
                "          <a:Operator>Equal</a:Operator>\n" +
                "          <a:Relation>And</a:Relation>\n" +
                "        </a:QueryStructure>\n" +
                "        <a:QueryStructure>\n" +
                "          <a:DataType>Boolean</a:DataType>\n" +
                "          <a:FieldName>fIsActive</a:FieldName>\n" +
                "          <a:FieldValue>True</a:FieldValue>\n" +
                "          <a:Operator>Equal</a:Operator>\n" +
                "          <a:Relation>And</a:Relation>\n" +
                "        </a:QueryStructure>\n" +
                "        <a:QueryStructure>\n" +
                "          <a:DataType>String</a:DataType>\n" +
                "          <a:FieldName>isLotManage</a:FieldName>\n" +
                "          <a:FieldValue>False</a:FieldValue>\n" +
                "          <a:Operator>Equal</a:Operator>\n" +
                "          <a:Relation>And</a:Relation>\n" +
                "        </a:QueryStructure>\n" +
                "      </queryConditions>\n" +
                "      <orderFields xmlns:a=\"Gysoft.Wms.Core.LinqQuery\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" i:nil=\"true\"/>\n" +
                "      <currentPage>1</currentPage>\n" +
                "      <pageSize>50</pageSize>\n" +
                "    </CallSearchMethod>\n" +
                "  </s:Body>\n" +
                "</s:Envelope>";
        return postWms(param);
    }










    public static JSONArray postWms(String param){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.TEXT_XML);
        httpHeaders.set("SOAPAction","http://tempuri.org/Gateway/CallSearchMethod");

        HttpEntity<Object> requestEntity = new HttpEntity<Object>(param.getBytes(), httpHeaders);
        String res = restTemplate.postForObject(WMS_URL,requestEntity,String.class);
        try {
            Document document = DocumentHelper.parseText(res);
            Element rootElement = document.getRootElement().element("Body").element("CallSearchMethodResponse").element("CallSearchMethodResult");
            Element RecordElement = rootElement.element("RecordString");
            String recordString = RecordElement.getStringValue();

            JSONArray root = JSON.parseArray(recordString);

            //System.out.println(root.getString("fLoginName"));
            //System.out.println(root.getString("fSessionID"));
            //System.out.println(root.getString("fXmlTree"));
            return root;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
